package view;

import controllers.ChatInterfaceControllers;
import entity.User;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import jdbc.Function;
import util.MessageVerticalBox;
import java.util.ArrayList;

/**
 * @author zjw
 * @date 2020/1/4 9:46
 */
public class ChatInterface {
    private User user;
    private Stage primaryStage;
    /**
     * 好友列表
     */
    private ObservableList<Label> friends = FXCollections.observableArrayList();
    private ListView<Label> friendsListview;
    /**
     * 群聊布局窗口即其组件
     */
    private Pane chatPane;
    private ListView chatBoxListView;
    private TextArea messageEditingTextArea;
    private Button sendOutButton;
    /**
     * 个人信息列表
     */
    private MenuBar menuBar;
    private Menu personalMenu;
    private MenuItem myInforMenuItem;
    private MenuItem switchAccountMenuItem;
    private MenuItem exitAccountMenuItem;
    // 总布局窗格
    private AnchorPane anchorPane;

    /**
     * 将数据库中存在的好友列表传到链表中
     */
    {
        Function function = new Function();
        ArrayList<String> allId = function.getAllId();
        for(String e : allId) {
            Label friendButton = ChatInterface.getFriendLabel(function.findUser(e));
            friends.add(friendButton);
        }
    }

    /**
     * 初始化各个布局和组件
     * @param user
     */
    public ChatInterface(User user, Stage primaryStage) {
        this.user = user;
        this.primaryStage = primaryStage;
        // 好友列表
        friendsListview = new ListView<Label>(friends);
        /**
         * 群聊界面的布局和组件
         */
        chatPane = new Pane();
        chatBoxListView = new ListView();
        // messageEditingTextArea实例化一定要在控制对象实例化之前，否则传入的到按钮上的信息为空
        messageEditingTextArea = new TextArea();
        // 实例化一个控制器对象
        ChatInterfaceControllers chatControllers = new ChatInterfaceControllers(user, primaryStage, messageEditingTextArea, chatBoxListView);
        // 返回一个有发送功能的按钮
        sendOutButton = chatControllers.getSendOutButton();
        /**
         * 个人信息菜单
         */
        menuBar = new MenuBar();
        personalMenu = new Menu("", new ImageView(new Image(getClass().getResourceAsStream("../image/ok.png"))));
        myInforMenuItem = chatControllers.getMyInforMenuItem();
        switchAccountMenuItem = chatControllers.getSwitchAccountMenuItem();
        exitAccountMenuItem = chatControllers.getExitAccountMenuItem();
    }


    /**
     * 需要重新打开的一个窗口
     */
    public void start() {
        initInterface();
        Scene scene = new Scene(anchorPane, 1000, 800);
        scene.getStylesheets().add(ChatInterface.class.getResource("css/ChatInterfaceCss.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("Chat Room");
        primaryStage.setResizable(false);
        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("../image/Home.jpg")));
        primaryStage.show();
    }

    /**
     * 将接受到的信息直接传到此时的chatBoxListView上，即聊天框上
     * @param id 接收的信息的好友id
     * @param message 接受到的信息
     * @param jud 判断接受到信息时自己的还是好友的
     */
    public void sendMessageToChatBoxListView(String id, String message, int jud) {
        VBox messageVerticalBox = MessageVerticalBox.getMessageVerticalBox(id, message, jud);
        chatBoxListView.getItems().add(messageVerticalBox);
    }

    /**
     * 好友列表好友按钮，获得好友个人信息或打开好友聊天室
     * @param friend
     * @return
     */
    private static Label getFriendLabel(User friend) {
        Label friendLabel = new Label(friend.getId());
        // 右击查看好友信息，单机打开聊天
        friendLabel.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                MouseButton button = event.getButton();
                if(button == MouseButton.SECONDARY){
                    try {
                        new PersonalInformationInterface(friend, new Stage(), 0).start();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if(button == MouseButton.PRIMARY) {
                        try {
                            new ChatBoxInterface(friend, new Stage()).start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                }
            }
        });
        return friendLabel;
    }

    /**
     * 初始化界面各组件的位置信息，大小
     */
    private void initInterface() {
        chatPane.setPrefHeight(750);
        chatPane.setPrefWidth(800);
        chatPane.setLayoutX(200);
        chatPane.setLayoutY(50);

        chatBoxListView.setPrefWidth(810);
        chatBoxListView.setPrefHeight(600);
        chatBoxListView.setLayoutX(0);
        chatBoxListView.setLayoutY(0);

        messageEditingTextArea.setPrefWidth(810);
        messageEditingTextArea.setPrefHeight(160);
        messageEditingTextArea.setLayoutX(0);
        messageEditingTextArea.setLayoutY(600);
        messageEditingTextArea.setWrapText(true);

        sendOutButton.setLayoutX(730);
        sendOutButton.setLayoutY(700);

        chatPane.getChildren().addAll(messageEditingTextArea, chatBoxListView, sendOutButton);

        friendsListview.setLayoutX(0);
        friendsListview.setLayoutY(50);
        friendsListview.setPrefWidth(200);
        friendsListview.setPrefHeight(760);

        menuBar.setLayoutX(5);
        menuBar.setLayoutY(5);
        personalMenu.getItems().addAll(myInforMenuItem, switchAccountMenuItem, new SeparatorMenuItem(), exitAccountMenuItem);
        menuBar.getMenus().add(personalMenu);

        anchorPane = new AnchorPane();
        anchorPane.getChildren().addAll(friendsListview, menuBar);
        anchorPane.getChildren().add(chatPane);
    }
}
